package com.yudaob2b.elong.task;

import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.entity.po.ElongHotelRatePO;
import com.yudaob2b.business.service.ElongHotelInventoryService;
import com.yudaob2b.business.service.ElongHotelRateService;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.common.utils.ListUtil;
import com.yudaob2b.elong.constant.Constant;
import com.yudaob2b.elong.entity.vo.Inventory;
import com.yudaob2b.elong.entity.vo.Rate;
import com.yudaob2b.elong.entity.vo.result.HotelDataInventoryResult;
import com.yudaob2b.elong.entity.vo.result.HotelDataRateResult;
import com.yudaob2b.elong.enums.EnumPaymentType;
import com.yudaob2b.elong.service.HotelInventoryService;
import com.yudaob2b.elong.service.HotelRateService;
import com.yudaob2b.elong.service.HttpStaticService;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/20/18 8:42 PM
 */
public class DataInvLocalTask implements Runnable, Serializable {
    private static Logger dataInvLocalTaskLogger = LoggerFactory.getLogger("dataInvLocalTaskLogger");

    /**
     * 酒店id，最多10个
     */
    private List<String> hotelIds;
    private HttpStaticService httpStaticService;
    private HotelInventoryService hotelInventoryService;
    private ThreadPoolTaskExecutor dataInvDbTaskExecutor;
    private ElongHotelInventoryService elongHotelInventoryService;
    private RedisTemplate redisTemplate;
    private ClientInterface clientUtil;
    // 线程添加/更新列表数
    private final int TASK_LIST_COUNT = 500;

    public DataInvLocalTask(List<String> hotelIds, HttpStaticService httpStaticService
            , HotelInventoryService hotelInventoryService, ThreadPoolTaskExecutor dataInvDbTaskExecutor
            , ElongHotelInventoryService elongHotelInventoryService, RedisTemplate redisTemplate, ClientInterface clientUtil) {
        this.hotelIds = hotelIds;
        this.httpStaticService = httpStaticService;
        this.hotelInventoryService = hotelInventoryService;
        this.dataInvDbTaskExecutor = dataInvDbTaskExecutor;
        this.elongHotelInventoryService = elongHotelInventoryService;
        this.redisTemplate = redisTemplate;
        this.clientUtil = clientUtil;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        if (hotelIds.size() <= 10) {
            StringBuilder hotelIdBuilder = new StringBuilder();
            try {
                // 查询开始时间，当天时间
                Date startDate = new Date();
                // 查询结束时间，90天后的时间
                Date endDate = DateUtil.rollDay(startDate, Constant.INCR_DAYS);

                for (String hotelId : hotelIds) {
                    hotelIdBuilder.append(hotelId).append(",");
                }

                // 10个酒店id查询一次
                HotelDataInventoryResult result = httpStaticService.getHotelDataInventory(hotelIdBuilder.toString(), null, null, startDate, endDate, null);
                // 重置查询的酒店id
                if (result != null && "0".equals(result.getCode()) && result.getResult().getInventories().size() > 0) {
                    List<List<Inventory>> listPage = ListUtil.split(result.getResult().getInventories(), TASK_LIST_COUNT);
                    for (List<Inventory> invList : listPage) {
                        DataInvLocalDbTask dbTask = new DataInvLocalDbTask(invList, elongHotelInventoryService, redisTemplate, hotelInventoryService,clientUtil);
                        dataInvDbTaskExecutor.execute(dbTask);
                    }
                }
            } catch (Exception e) {
                dataInvLocalTaskLogger.info("全量库存落地线程异常, {}：", hotelIdBuilder.toString(), e);
                e.printStackTrace();
            }

        }
        //dataInvLocalTaskLogger.info("---库存落地线程总耗时：---{}", System.currentTimeMillis() - start);
    }
}
